home *** CD-ROM | disk | FTP | other *** search
- Path: news.crystalball.com!news
- From: Larry Weiss <lfw@oc.com>
- Newsgroups: comp.lang.c
- Subject: Re: Question on string literals and char arrays
- Date: Fri, 23 Feb 1996 12:17:38 -0600
- Organization: crystalball.com
- Message-ID: <312E04C2.71E3@oc.com>
- References: <KEITHBAR.96Feb22192211@owl.WPI.EDU>
- NNTP-Posting-Host: external.oc.com
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (Win16; I)
- CC: lfw@oc.com
-
- Keith Barrett wrote:
- >
- > Could someone explain to me in very great detail what is the
- > difference between: [see examples copied below]
-
-
- First example:
- ==============
- main()
- {
- char *s;
- s = "Keith";
- printf("%s\n", s);
- }
-
- allocated objects:
-
- o function named "main"
- o char pointer named "s"
- o a string containing "Keith"
- o a string containing "%s\n"
- o an unnamed char pointer to "%s\n" to assist the call to printf()
-
- semantics:
-
- o modify "s" to point to the string containing "Keith"
- o call printf() with two arguments, the first a char pointer to "%s\n" that
- is automatically allocated, and the second is the char pointer "s"
-
- Second example:
- ===============
- main()
- {
- char s[6];
- s = "Keith";
- printf("%s\n", s);
- }
-
- allocated objects:
-
- o function named "main"
- o a char named "s[0]"
- o a char named "s[1]" allocated contiguous to s[0]
- o a char named "s[2]" allocated contiguous to s[1]
- o a char named "s[3]" allocated contiguous to s[2]
- o a char named "s[4]" allocated contiguous to s[3]
- o a char named "s[5]" allocated contiguous to s[4]
- o a string containing "Keith"
- o a string containing "%s\n"
- o an unnamed char pointer to "%s\n" to assist the call to printf()
- o an unnamed char pointer to s[0] to assist the call to printf()
-
- semantics:
-
- o modify a non-existant char pointer "s" to point to the string containing "Keith"
- o call printf() with two arguments, the first a char pointer to "%s\n" that
- is automatically allocated, and the second is a char pointer to s[0]
- that is automatically allocated
-
- The second example tries to manipulate a nonexistant char pointer named "s".
- This is the bug.
-